﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using SADev.Gallery2.Protocol;
using System.Collections.ObjectModel;
using Microsoft.Office.Tools.Excel;

namespace g2_Album_Info
{
    public partial class Ribbon : OfficeRibbon
    {
        public Ribbon()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the Ribbon control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Office.Tools.Ribbon.RibbonUIEventArgs"/> instance containing the event data.</param>
        private void Ribbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        /// <summary>
        /// Handles the Click event of the getAlbumsButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs"/> instance containing the event data.</param>
        private void getAlbumsButton_Click(object sender, RibbonControlEventArgs e)
        {
            string host;
            string username;
            string password;
            string authToken;

            using (connectionInfoForm connectionInfo = new connectionInfoForm())
            {
                connectionInfo.ShowDialog();
                host = connectionInfo.host;
                username = connectionInfo.username;
                password = connectionInfo.password;
            }

            Actions actions = new Actions(host);
            if (actions.NoOp().Status != Status.GR_STAT_SUCCESS)
            {
                throw new Exception("Cannot connect to server");
            }

            Microsoft.Office.Interop.Excel.Worksheet worksheet = Globals.ThisAddIn.GetActiveWorksheet();

            worksheet.Cells[1, 1] = "Title";
            worksheet.Cells[1, 2] = "Name";
            worksheet.Cells[1, 3] = "Id";
            worksheet.Cells[1, 4] = "Max Size";
            worksheet.Cells[1, 5] = "Auto Resize";
            worksheet.Cells[1, 6] = "Add to beginning";
            worksheet.Cells[1, 7] = "Can Add";
            worksheet.Cells[1, 8] = "Can Create Sub Albums";
            worksheet.Cells[1, 9] = "Can Delete Album";
            worksheet.Cells[1, 10] = "Can Delete Item";
            worksheet.Cells[1, 11] = "Can Write";
            worksheet.Cells[1, 12] = "Max Size";
            worksheet.Cells[1, 13] = "Parent";
            worksheet.Cells[1, 14] = "Resize Size";
            worksheet.Cells[1, 15] = "Summary";
            worksheet.Cells[1, 16] = "Thumbnail Size";

            authToken = actions.LogOn(username, password).AuthToken;
            Collection<Album> albums = actions.FetchAlbumsPrune(authToken).Albums;
            for (int counter = 0; counter < albums.Count; counter++)
            {
                worksheet.Cells[counter + 2, 1] = albums[counter].Title;
                worksheet.Cells[counter + 2, 2] = albums[counter].Name;
                worksheet.Cells[counter + 2, 3] = albums[counter].Id;
                worksheet.Cells[counter + 2, 4] = actions.GetAlbumProperties(authToken, albums[counter].Name).MaxSize;
                worksheet.Cells[counter + 2, 5] = actions.GetAlbumProperties(authToken, albums[counter].Name).AutoResize;
                worksheet.Cells[counter + 2, 6] = actions.GetAlbumProperties(authToken, albums[counter].Name).AddToBeginning;
                worksheet.Cells[counter + 2, 7] = albums[counter].CanAdd;
                worksheet.Cells[counter + 2, 8] = albums[counter].CanCreateSubAlbums;
                worksheet.Cells[counter + 2, 9] = albums[counter].CanDeleteAlbum;
                worksheet.Cells[counter + 2, 10] = albums[counter].CanDeleteItem;
                worksheet.Cells[counter + 2, 11] = albums[counter].CanWrite;
                worksheet.Cells[counter + 2, 12] = albums[counter].MaxSize;
                worksheet.Cells[counter + 2, 13] = albums[counter].Parent;
                worksheet.Cells[counter + 2, 14] = albums[counter].ResizeSize;
                worksheet.Cells[counter + 2, 15] = albums[counter].Summary;
                worksheet.Cells[counter + 2, 16] = albums[counter].ThumbnailSize;
            }
        }

        /// <summary>
        /// Handles the Click event of the getImageInfoButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs"/> instance containing the event data.</param>
        private void getImageInfoButton_Click(object sender, RibbonControlEventArgs e)
        {
            string host;
            string username;
            string password;
            string authToken;

            using (connectionInfoForm connectionInfo = new connectionInfoForm())
            {
                connectionInfo.ShowDialog();
                host = connectionInfo.host;
                username = connectionInfo.username;
                password = connectionInfo.password;
            }

            Actions actions = new Actions(host);
            if (actions.NoOp().Status != Status.GR_STAT_SUCCESS)
            {
                throw new Exception("Cannot connect to server");
            }


            authToken = actions.LogOn(username, password).AuthToken;
            Collection<Album> albums = actions.FetchAlbumsPrune(authToken, true).Albums;

            List<string> albumTitles = new List<string>(albums.Select(a => a.Title));

            string selectedAlbum;

            using (ImageInfoRequestForm imageInfoRequestForm = new ImageInfoRequestForm())
            {
                imageInfoRequestForm.Albums = albumTitles;
                imageInfoRequestForm.Fill();
                imageInfoRequestForm.ShowDialog();
                selectedAlbum = imageInfoRequestForm.SelectedAlbum;
            }

            string albumName = (from a in albums
                                where a.Title == selectedAlbum
                                select a.Name).First();

            Microsoft.Office.Interop.Excel.Worksheet worksheet = Globals.ThisAddIn.GetActiveWorksheet();
            worksheet.Cells[1, 1] = "Title";
            worksheet.Cells[1, 2] = "Caption";
            worksheet.Cells[1, 3] = "Clicks";
            worksheet.Cells[1, 4] = "Extension";
            worksheet.Cells[1, 5] = "Hidden";
            worksheet.Cells[1, 6] = "Id";
            worksheet.Cells[1, 7] = "Raw: Filename";
            worksheet.Cells[1, 8] = "Raw: Filesize";
            worksheet.Cells[1, 9] = "Raw: Height";
            worksheet.Cells[1, 10] = "Raw: Width";
            worksheet.Cells[1, 11] = "Raw: URL";
            worksheet.Cells[1, 12] = "Risized: Filename";
            worksheet.Cells[1, 13] = "Risized: Filesize";
            worksheet.Cells[1, 14] = "Risized: Height";
            worksheet.Cells[1, 15] = "Risized: Width";
            worksheet.Cells[1, 16] = "Thumbnail: Filename";
            worksheet.Cells[1, 17] = "Thumbnail: Filesize";
            worksheet.Cells[1, 18] = "Thumbnail: Height";
            worksheet.Cells[1, 19] = "Thumbnail: Width";

            Collection<Image> images = actions.FetchImages(authToken, albumName).Images;
            for (int counter = 0; counter < images.Count; counter++)
            {
                Image image = images[counter];
                worksheet.Cells[counter + 2, 1] = image.Title;
                worksheet.Cells[counter + 2, 2] = image.Caption;
                worksheet.Cells[counter + 2, 3] = image.Clicks;
                worksheet.Cells[counter + 2, 4] = image.Extension;
                worksheet.Cells[counter + 2, 5] = image.Hidden;
                worksheet.Cells[counter + 2, 6] = image.Id;
                worksheet.Cells[counter + 2, 7] = image.RawDimensions.FileName;
                worksheet.Cells[counter + 2, 8] = image.RawDimensions.FileSize;
                worksheet.Cells[counter + 2, 9] = image.RawDimensions.Height;
                worksheet.Cells[counter + 2, 10] = image.RawDimensions.Width;
                worksheet.Cells[counter + 2, 11] = image.RawImageUrl.ToString().Replace("/", "//");
                worksheet.Cells[counter + 2, 12] = image.ResizedDimensions.FileName;
                worksheet.Cells[counter + 2, 13] = image.ResizedDimensions.FileSize;
                worksheet.Cells[counter + 2, 14] = image.ResizedDimensions.Height;
                worksheet.Cells[counter + 2, 15] = image.ResizedDimensions.Width;
                worksheet.Cells[counter + 2, 16] = image.ThumbnailDimensions.FileName;
                worksheet.Cells[counter + 2, 17] = image.ThumbnailDimensions.FileSize;
                worksheet.Cells[counter + 2, 18] = image.ThumbnailDimensions.Height;
                worksheet.Cells[counter + 2, 19] = image.ThumbnailDimensions.Width;
            }

        }
    }
}
